package com.zmn.oms.services.impl.orderappbind;

import com.zmn.oms.model.entity.orderappbind.OrderThirdAppBind;
import com.zmn.oms.persistence.interfaces.orderappbind.OrderThirdAppBindDao;
import com.zmn.oms.services.interfaces.orderappbind.OrderThirdAppBindService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 类描述：
 *
 * @author yule
 * @date 2020/12/25 14:21
 */
@Service
public class OrderThirdAppBindServiceImpl implements OrderThirdAppBindService {

    @Resource
    private OrderThirdAppBindDao orderThirdAppBindDao;

    @Override
    public int insertOrderThirdApp(OrderThirdAppBind orderThirdAppBind) {
        OrderThirdAppBind orderThirdApp = findByOrderIdAndPlatSrcMaster(orderThirdAppBind.getOrderId(), orderThirdAppBind.getPlatWork());
        if (orderThirdApp == null) {
            return orderThirdAppBindDao.insert(orderThirdAppBind);
        }
        return 0;
    }

    @Override
    public OrderThirdAppBind findByOrderIdAndPlatSrcMaster(Long orderId, Integer platWork) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderThirdAppBindDao.findByOrderIdAndPlat(orderId, platWork);
        }
    }

    @Override
    public OrderThirdAppBind findByOrderIdAndPlat(Long orderId, Integer platWork) {
        OrderThirdAppBind orderThirdAppBind = orderThirdAppBindDao.findByOrderIdAndPlat(orderId, platWork);
        return orderThirdAppBind;
    }
}
